package com.zzh.lib.views;

import android.content.Context;
import android.content.res.TypedArray;
import android.os.CountDownTimer;
import android.util.AttributeSet;

import com.zzh.lib.dialog.R;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.widget.AppCompatTextView;

/**
 * Created by ZZH on 2021/6/3.
 *
 * @Date: 2021/6/3
 * @Email: zzh_hz@126.com
 * @QQ: 1299234582
 * @Author: zzh
 * @Description: 倒计时的View
 */
public class HCountDownTextView extends AppCompatTextView {
    long millisInFuture = 60000, countDownInterval = 1000;

    private HCountDownCallback mCountDownCallback;

    public HCountDownTextView(@NonNull Context context) {
        super(context);
        init(context, null);
    }

    public HCountDownTextView(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);

        init(context, attrs);

    }


    public HCountDownTextView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs);
    }

    public void setCountDownCallback(HCountDownCallback countDownCallback) {
        mCountDownCallback = countDownCallback;
    }

    public long getMillisInFuture() {
        return millisInFuture;
    }


    public long getCountDownInterval() {
        return countDownInterval;
    }

    /**
     * 设置倒计时时间和间隔
     *
     * @param millisInFuture
     */
    public void setMIFAndCDI(long millisInFuture, long countDownInterval) {
        checkArgs(millisInFuture, countDownInterval);

        this.millisInFuture = millisInFuture;
        this.countDownInterval = countDownInterval;
    }

    private void checkArgs(long millisInFuture, long countDownInterval) {
        if (millisInFuture <= 0 || countDownInterval <= 0 || millisInFuture < countDownInterval) {
            throw new RuntimeException("设置倒计时时间和间隔 参数错误：要求millisInFuture > 0 && countDownInterval > 0 && countDownInterval >= millisInFuture");
        }
    }

    private void init(Context context, AttributeSet attrs) {
        if (attrs != null) {
            TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.HCountDownTextView);
            millisInFuture = ta.getInteger(R.styleable.HCountDownTextView_zh_millis_in_future, 60000);
            countDownInterval = ta.getInteger(R.styleable.HCountDownTextView_zh_count_down_interval, 1000);
            ta.recycle();
            checkArgs(millisInFuture, countDownInterval);
        }
        super.setOnClickListener(v -> {
            boolean clickContinue = true;
            if (mCountDownCallback != null) {
                clickContinue = mCountDownCallback.onClick(HCountDownTextView.this);
            }
            if (clickContinue) {
                startTimer();
            }
        });
    }

    private CountDownTimer mCountDownTimer = null;

    public abstract static class HCountDownCallback {
        /**
         * 倒计时事件
         *
         * @param millisUntilFinished 剩余时间
         * @param textView            控件
         */
        public abstract void onTick(HCountDownTextView textView, long millisUntilFinished);


        /**
         * 倒计时结束
         *
         * @param textView 点击的实例
         */
        public abstract void onFinish(HCountDownTextView textView);

        /**
         * 点击事件
         *
         * @param textView 点击的实例
         */
        public boolean onClick(HCountDownTextView textView) {
            return true;
        }

        /**
         * 取消事件
         *
         * @param textView 点击的实例
         */
        public void onCancel(HCountDownTextView textView) {

        }

    }

    @Override
    public void setOnClickListener(@Nullable OnClickListener l) {

    }

    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        cancelTimer();
        mCountDownTimer = null;
    }


    /**
     * 开始定时任务
     */
    public void startTimer() {
        startTimer(millisInFuture, countDownInterval);
        setEnabled(false);
    }

    public void startTimer(long millisInFuture, long countDownInterval) {
        if (mCountDownTimer == null) {
            if (mCountDownTimer == null) {
                mCountDownTimer = new CountDownTimer(millisInFuture, countDownInterval) {
                    @Override
                    public void onTick(long millisUntilFinished) {
                        if (mCountDownCallback != null) {
                            mCountDownCallback.onTick(HCountDownTextView.this, millisUntilFinished);
                        }
                        setEnabled(false);
                    }

                    @Override
                    public void onFinish() {
                        if (mCountDownCallback != null) {
                            mCountDownCallback.onFinish(HCountDownTextView.this);
                        }
                        setEnabled(true);
                    }
                };
            }
        }
        setEnabled(false);
        mCountDownTimer.start();
    }

    /**
     * 取消定时任务
     */
    public void cancelTimer() {
        if (mCountDownTimer != null) {
            mCountDownTimer.cancel();
            if (mCountDownCallback != null) {
                mCountDownCallback.onCancel(this);
            }
        }
        setEnabled(true);
    }
}
